package com.android.baseconfig.common.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.baseconfig.R;
import com.android.baseconfig.base.BaseConfigApplication;
import com.android.baseconfig.common.entity.parameter.ShowDialogParameter;
import com.android.baseconfig.common.views.AppBasicDialog;

import androidx.annotation.StringRes;


public class DialogUtils {

    /**
     * 双按钮对话框
     *
     * @param context
     * @param showDialogParameter
     * @param positiveListener
     * @param negativeListener
     * @return
     */
    public static AppBasicDialog opDialog(final Context context,
                                          final ShowDialogParameter showDialogParameter,
                                          final DialogInterface.OnClickListener positiveListener,
                                          final DialogInterface.OnClickListener negativeListener) {
        final AppBasicDialog.Builder builder = new AppBasicDialog.Builder(context, false);
        builder.setMessage(showDialogParameter.getMsg());
        builder.setTitle(showDialogParameter.getTitle());
        builder.setPositiveButton(showDialogParameter.getStrYes(), positiveListener);
        builder.setNegativeButton(showDialogParameter.getStrNo(), negativeListener);
        return builder.create();
    }

    /**
     * 单确认按钮对话框
     *
     * @param context
     * @param titleRes
     * @param msgRes
     * @param positiveRes
     * @param listener
     * @return
     */
    public static AppBasicDialog opDialog(final Context context, final int titleRes,
                                          final int msgRes, final int positiveRes,
                                          final DialogInterface.OnClickListener listener) {
        return opDialog(context, context.getString(titleRes), context.getString(msgRes),
                context.getString(positiveRes), listener);
    }

    /**
     * 单确认按钮对话框
     *
     * @param context
     * @param title
     * @param msg
     * @param positive
     * @param listener
     * @return
     */
    public static AppBasicDialog opDialog(final Context context, final String title,
                                          final String msg, final String positive,
                                          final DialogInterface.OnClickListener listener) {
        final AppBasicDialog.Builder builder = new AppBasicDialog.Builder(context, false);
        builder.setMessage(msg);
        builder.setTitle(title);
        builder.setPositiveButton(positive, listener);
        final AppBasicDialog create = builder.create();
        create.setCanceledOnTouchOutside(false);
        create.setCancelable(false);
        return create;
    }

    /**
     * 双按钮对话框
     *
     * @param context
     * @param msgRes
     * @param titleRes
     * @param positiveRes
     * @param negativeRes
     * @param positiveListener
     * @return
     */
    public static AppBasicDialog opDialog(final Context context, int msgRes, int titleRes,
                                          int positiveRes, int negativeRes,
                                          final DialogInterface.OnClickListener positiveListener) {
        return opDialog(context, context.getString(msgRes), context.getString(titleRes),
                context.getString(positiveRes), context.getString(negativeRes), positiveListener);
    }

    /**
     * 双按钮对话框
     *
     * @param context
     * @param msg
     * @param title
     * @param positive
     * @param negative
     * @param positiveListener
     * @return
     */
    public static AppBasicDialog opDialog(final Context context, String msg, String title,
                                          String positive, String negative,
                                          final DialogInterface.OnClickListener positiveListener) {
        final AppBasicDialog.Builder builder = new AppBasicDialog.Builder(context, false);
        builder.setMessage(msg);
        builder.setTitle(title);
        builder.setPositiveButton(positive, positiveListener);
        builder.setNegativeButton(negative, new DismissDialog());
        return builder.create();
    }

    /**
     * 固定文案，标题：提示，确认文案：确定，取消文案：取消
     *
     * @param context
     * @param rMsg
     * @param positiveListener
     * @return
     */
    public static AppBasicDialog easyTipsDialog(final Context context, @StringRes int rMsg,
                                                final DialogInterface.OnClickListener positiveListener) {
        final AppBasicDialog.Builder builder = new AppBasicDialog.Builder(context, false);
        builder.setMessage(rMsg);
        builder.setTitle(R.string.notice);
        builder.setPositiveButton(R.string.sure, positiveListener);
        builder.setNegativeButton(R.string.cancel, new DismissDialog());
        AppBasicDialog appBasicDialog = builder.create();
        appBasicDialog.show();
        return appBasicDialog;
    }

    /**
     * 简单的提示，单确认按钮
     *
     * @param context
     * @param titleRes
     * @param msgRes
     * @param positiveRes
     * @return AppBasicDialog
     */
    public static AppBasicDialog simpleShowDialog(final Context context, final int titleRes,
                                                  final int msgRes, final int positiveRes) {
        final AppBasicDialog.Builder builder = new AppBasicDialog.Builder(context, false);
        builder.setMessage(context.getString(msgRes));
        builder.setTitle(context.getString(titleRes));
        builder.setPositiveButton(context.getString(positiveRes), new DismissDialog());
        return builder.create();
    }

    public static Dialog createLoadingDialog(Context context, String title) {
        View localView = LayoutInflater.from(context).inflate(R.layout.dialog_create_loading, null);
        LinearLayout localLinearLayout = (LinearLayout) localView.findViewById(R.id.dialog_view);
        TextView textView = localView.findViewById(R.id.tipTextView);
        if (TextUtils.isEmpty(title)) {
            textView.setVisibility(View.GONE);
            localLinearLayout.setPadding(context.getResources().getDimensionPixelSize(R.dimen.dp_16),
                    context.getResources().getDimensionPixelSize(R.dimen.dp_16),
                    context.getResources().getDimensionPixelSize(R.dimen.dp_16),
                    context.getResources().getDimensionPixelSize(R.dimen.dp_16));
        } else {
            textView.setVisibility(View.GONE);
            textView.setText(title);
        }
        /*ImageView iv = (ImageView) localView.findViewById(R.id.iv_loading);
        iv.setBackgroundResource(R.drawable.robot_loading_anim);
        AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
        anim.start();*/
        Dialog dialog = new Dialog(context, R.style.loading_dialog);
        dialog.setCancelable(true);
        dialog.setContentView(localLinearLayout, new LinearLayout.LayoutParams(-1, -1));
        return dialog;
    }

    public static void dataLoading(final TextView textView, final String text,
                                   final ProgressBar progressBar, final int type) {
        switch (type) {
            case 1:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                }, 0L);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                }, 10L);
                break;
            case 2:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                }, 10L);
                break;
            case 3:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(text);
                        progressBar.setVisibility(View.GONE);
                    }
                }, 0L);
                break;
            case 4:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setVisibility(View.GONE);
                    }
                }, 0L);
                break;
            default:
                break;
        }
    }

    public static void dataLoading(final TextView textView, final int id,
                                   final ProgressBar progressBar, final int type) {
        dataLoading(textView, BaseConfigApplication.getInstance().getString(id), progressBar, type);
    }
}
